package student.aistar.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/10/28 10:01
 */
@Configuration
public class RabbitMQConfig {
    //声明4个路由,key 4个队列 2个交换机

    /*
        延迟部分
    * */

    //延迟交换机
    public static final String DELAY_EXCHANGE_NAME = "delay.exchange";

    //延迟队列
    public static final String DELAY_QUEUE_A_NAME = "delay.queue.a";
    public static final String DELAY_QUEUE_B_NAME = "delay.queue.b";

    //延迟队列路由 key
    public static final String DELAY_QUEUE_A_ROUTING_KEY = "delay.queue.a.routingKey";
    public static final String DELAY_QUEUE_B_ROUTING_KEY = "delay.queue.b.routingKey";


    /*
        死信部分
     */
    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead.letter.exchange";

    //死信队列
    public static final String DEAD_LETTER_QUEUE_A_NAME = "dead.queue.a";
    public static final String DEAD_LETTER_QUEUE_B_NAME = "dead.queue.b";

    //死信队列路由 key
    public static final String DEAD_LETTER_QUEUE_A_ROUTING_KEY = "dead.letter.delay_10s.routingKey";
    public static final String DEAD_LETTER_QUEUE_B_ROUTING_KEY = "dead.letter.delay_60s.routingKey";

    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }

    //声明延迟队列队列A, 延迟10s, 并且绑定到对应的死信交换机
    @Bean("delayQueueA")
    public Queue delayQueueA(){
        Map<String, Object> args = new HashMap<>();
        //x-dead-letter-exchange 声明队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        //x-dead-letter-exchange 声明队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_A_ROUTING_KEY);
        //声明队列的消息 TTL存活时间
        args.put("x-message-ttl", 60000);
        return QueueBuilder.durable(DELAY_QUEUE_A_NAME).withArguments(args).build();
    }

    //声明延迟队列队列B, 延迟60s, 并且绑定到对应的死信交换机
    @Bean("delayQueueB")
    public Queue delayQueueB(){
        Map<String, Object> args = new HashMap<>();
        //x-dead-letter-exchange 声明队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        //x-dead-letter-exchange 声明队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_B_ROUTING_KEY);
        //声明队列的消息 TTL存活时间
        args.put("x-message-ttl", 60000);
        return QueueBuilder.durable(DELAY_QUEUE_B_NAME).withArguments(args).build();
    }

    //声明延迟队列A的绑定关系
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueueA") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_A_ROUTING_KEY);
    }

    //声明延迟队列B的绑定关系
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueueB") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_B_ROUTING_KEY);
    }

//    //声明死信队列A的绑定关系
//    @Bean("deadLetterQueueA")
//    public Queue deadLetterQueueA(){
//        Map<String, Object> args = new HashMap<>();
//        //x-dead-letter-exchange 声明队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
//        //x-dead-letter-exchange 声明队列的死信路由key
//        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_B_ROUTING_KEY);
//        //声明队列的消息 TTL存活时间
//        args.put("x-message-ttl", 60000);
//        return QueueBuilder.durable(DELAY_QUEUE_B_NAME).withArguments(args).build();
//    }
//    //声明死信队列B的绑定关系
//    @Bean("deadLetterQueueB")
//    public Queue deadLetterQueueB(){
//        Map<String, Object> args = new HashMap<>();
//        //x-dead-letter-exchange 声明队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
//        //x-dead-letter-exchange 声明队列的死信路由key
//        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_B_ROUTING_KEY);
//        //声明队列的消息 TTL存活时间
//        args.put("x-message-ttl", 60000);
//        return QueueBuilder.durable(DELAY_QUEUE_B_NAME).withArguments(args).build();
//    }

    //声明死信队列A
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_A_NAME);
    }

    //声明死信队列B
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB(){
        return new Queue(DEAD_LETTER_QUEUE_B_NAME);
    }
    //声明死信队列A的绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
                                 @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_A_ROUTING_KEY);
    }
    //声明死信队列B的绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
                                 @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_B_ROUTING_KEY);
    }
}
